<?php

    require_once '../funcoes.php';
    $conexao = new Conexao();

    $mark = $_POST;
    
    pre($_POST);

    $id_usuario = $_SESSION['usuario']['id'];
    $caduca	= "NULL";
    $privado	= 0;
    $some	= 0;

    $lat	= $mark['latitude'];
    $lng	= $mark['longitude'];
    $conteudo	= $mark['conteudo'];
    $icone	= $mark['icone'];
    $titulo	= $mark['titulo'];


    if(!empty($mark['caduca']))   
	$caduca = "'" . $mark['caduca'] . "'";

    if(!empty ($mark['some']))
	$some = $mark['some'];

    if(!empty ($mark['privado']))
	$privado = $mark['privado'];
    
    
    $query	= "INSERT INTO ofertas VALUES (default,'$lat','$lng','$conteudo','$icone',NOW(),'$titulo',$id_usuario,$caduca,$some,$privado)";
    $sucesso	= mysql_query($query);

    //echo $query;

    $idOferta = mysql_result(mysql_query("SELECT id FROM ofertas ORDER BY id DESC LIMIT 1"), 0, 'id');

    
    
    include('../php/uploadhelper.php');
		
    $names = uploadHelper::getVar('names');
    $files = uploadHelper::getVar('file_names');
    
    $contagem = 0;
    if($files)
    foreach($files as $k => $file) {
	
	$source = uploadHelper::decrypt($file);
	
	$type = explode(".", $names[$k]);
	$type = $type[count($type)-1];
	
	$nomeFoto = "foto-$idOferta-$contagem.$type";
	$destiny = "../../fotos/$nomeFoto";
	
	
	if(copy($source, $destiny)){
	    
	    $SQL_foto = "INSERT INTO foto (id, id_oferta, path) VALUES (default, $idOferta, '$nomeFoto')";
	    mysql_query($SQL_foto);
	}
	
	$contagem++;
	
    }
    
    if(!empty ($mark['quote'])){
	$id_destinatario = $mark['quote'];
	$sql_quote = "INSERT INTO quote VALUES ($idOferta,$id_destinatario,$id_usuario,NOW())";
	mysql_query($sql_quote);
	//echo $sql_quote;
    }
    
   
    
?>
